<?php
    /**
    * @brief EC m_user Model
    * @file /application/models/m_user.php
	* @author con100 <con100@playplus.net>
	* @since 18/04/2013
	* @copyright Copyright (c) 2013, PlayPlus
    **/
    class M_user extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }

		function get_user_data($uid)
		{
			$sql = "select no, name, level, money, cash, nation, king, exp, max_exp, hp, max_hp, timestemp_hp, en, max_en, timestemp_en, timestemp_rest, 
						timestemp_active, timestemp_free, timestemp_moon, sp_atk, sp_hit, sp_def, sp_spd, killer_no, comment from ebs_member where id=?";
			$res = $this->db->query($sql, array($uid));

			if($res->num_rows()<0){
				return $result = array("Result"=>FALSE);
			}
			else{
				$data = $res->row_array();
				return $result = array("Result"=>TRUE, "user_no"=>$data['no'], "nick"=>$data['name'], "level"=>$data['level'], "money"=>$data['money'], "cash"=>$data['cash'], "nation"=>$data['nation'], "king"=>$data['king'],
							"exp"=>$data['exp'], "max_exp"=>$data['max_exp'], "hp"=>$data['hp'], "max_hp"=>$data['max_hp'], "timestamp_hp"=>$data['timestemp_hp'], "en"=>$data['en'], "max_en"=>$data['max_en'], 
							"timestamp_en"=>$data['timestemp_en'], "timestamp_rest"=>$data['timestemp_rest'], "timestamp_active"=>$data['timestemp_active'], "timestamp_free"=>$data['timestemp_free'], 
							"timestamp_moon"=>$data['timestemp_moon'],"sp_atk"=>$data['sp_atk'], "sp_hit"=>$data['sp_hit'], "sp_def"=>$data['sp_def'], "sp_spd"=>$data['sp_spd'], "killer_no"=>$data['killer_no'], 
							"comment"=>$data['comment']);
			}

		}

		function recovery_hp($uid)
		{
			$sql = "select hp, timestemp_hp, max_hp, money from ebs_member where id=?";
			$res = $this->db->query($sql, array($uid));
			$data = $res->row_array();
			//hp 보정
			$time_hp = (int)($data['hp']+(time()-$data['timestemp_hp']) * $hp_sec);

			if($data['hp'] > 0){
				$data['hp'] = min($time_hp, $data['max_hp']);
			}else if($time_hp > $data['max_hp']){
				$data['hp'] = $data['max_hp'];
			}
			//회복비용
			$pay_recovery_hp = $this->eclib->_getRecoveryPay_($data);
			//체력회복
			if($data['hp'] < $data['max_hp'] && $pay_recovery_hp <= $data['money']){
				$data['hp'] = $data['max_hp'];
				$data['money'] -= $pay_recovery_hp;
				$time = time();
				$this->db->trans_start();
				$sql = "update ebs_member set hp=?, money=?, timestemp_active=? where id=?";
				$res = $this->db->query($sql, array($data['hp'],$data['money'],$time,$uid));
				$this->db->trans_complete();
				return $result = array("Result"=>TRUE, "money"=>$data['money'], "hp"=>$data['hp']);
			}
			else{
				return $result = array("Result"=>FALSE);
			}
		}

		function change_max_hp($uid)
		{
			$sql = "select max_hp, money, nation from ebs_member where id=?";
			$res = $this->db->query($sql, array($uid));
			$data = $res->row_array();
			//최대HP 증가 비용
			$pay_maxup =  $this->eclib->_getMaxHpPay_($data);

			if($data['money'] >= $pay_maxup){
				$data['money']-= $pay_maxup;
				$data['max_hp'] += $this->config->item('_max_hp_up_');
				$time = time();
				$this->db->trans_start();
				$sql = "update ebs_member set money=?, max_hp=?, timestemp_active =? where id=?";
				$res = $this->db->query($sql, array($data['money'],$data['max_hp'],$time,$uid));
				$this->db->trans_complete();
				return $result = array("Result"=>TRUE, "money"=>$data['money'], "max_hp"=> $data['max_hp']);
			}
			else{
				return $result = array("Result"=>FALSE);
			}
		}

		function change_stat($uid, $stat_a, $stat_b, $stat_c, $stat_d)
		{
			$sql = "select level from ebs_member where id=?";
			$res = $this->db->query($sql, array($uid));
			$data = $res->row_array();

			$check = $data['level'] * $this->config->item('stat_levelup');
			$sum = $stat_a + $stat_b + $stat_c + $stat_d;
			if($check >= $sum){
				$time = time();
				$this->db->trans_start();
				$sql = "update ebs_member set sp_atk=?, sp_hit=?, sp_def=?, sp_spd=?, timestemp_active=? where id=?";
				$res = $this->db->query($sql, array($stat_a,$stat_b,$stat_c,$stat_d,$time,$uid));
				$this->db->trans_complete();
				return $result = array("Result"=>TRUE);
			}else{
				return $result = array("Result"=>FALSE);
			}
		}

		function change_comment($uid, $comment)
		{
			$sql = "select no from ebs_member where id=?";
			$res = $this->db->query($sql, array($uid));

			if ($data->num_rows()>0) {
				$time = time();
				$sql = "update ebs_member set comment=?, timestemp_active = ? where id=?";
				$res = $this->db->query($sql, array($comment,$time,$uid));
				return $result = array("Result"=>TRUE);
			}
			else{
				return $result = array("Result"=>FALSE);
			}
		}
	}